﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.BLL;
using CMS.BLL.Entity;


public partial class AdminX_Banner_Banner : System.Web.UI.UserControl
{
    private int nDefaulanguage = Support.GetDefaultLanguage();
    protected void Page_Load(object sender, EventArgs e)
    {
        this.initDataPaging();
        Alert1.HideMessage();

        if (!IsPostBack)
        {
            this.ShowMess();

            this.BindDataToLanguageDropDown();
            this.SetMullanguage();
            this.SetStatusLanguage();
            this.BindDataToGrid(1);
        }
    }

    private void SetStatusLanguage()
    {
        int nCurrentLang = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        ddlStatus.DataSource = Support.dtbStatus(nCurrentLang);
        ddlStatus.DataTextField = "Description";
        ddlStatus.DataValueField = "Value";
        ddlStatus.DataBind();
    }
    private void SetMullanguage()
    {
        lblKeyword.Text = MultipleLanguage.LBL_KEYWORD[nDefaulanguage - 1];
        lblLanguage.Text = MultipleLanguage.LBL_LANGUAGE[nDefaulanguage - 1];
        lblStatus.Text = MultipleLanguage.LBL_STATUS[nDefaulanguage - 1];
        
        btnAddnew.Text = MultipleLanguage.BTN_ADDNEW[nDefaulanguage - 1];
        btnSearch.Text = MultipleLanguage.BTN_SEARCH[nDefaulanguage - 1];
    }
    #region Paging & Message
    private void initDataPaging()
    {
        Pager1.PageSize = Config.PAGESIZE;
        Pager1.ShowFirstLast = true;
        Pager1.FirstClause = "First";
        Pager1.LastClause = "Last";
        Pager1.NextClause = "Next";
        Pager1.OfClause = "of";
        Pager1.PageClause = "Page";
        Pager1.CompactedPageCount = 5;
        Pager1.PreviousClause = "Prev";
        Pager1.MaxSmartShortCutCount = 5;
        Pager1.NotCompactedPageCount = 5;
    }

    private void VisiblePaging(int intPagecount)
    {
        int nPageSize = 0;
        nPageSize = Config.PAGESIZE;
        if (intPagecount > nPageSize)
            Pager1.Visible = true;
        else
            Pager1.Visible = false;
    }

    public void pager_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
        Pager1.CurrentIndex = currnetPageIndx;
        BindDataToGrid(currnetPageIndx);
    }

    private void ShowMess()
    {
        int nMessage = ConvertData.ConvertToInt(Request.QueryString[Config.MESS_ID]);
        lblWarning.Text = MessagesAlert.SEARCH_NO_ITEM[nDefaulanguage - 1];
        switch (nMessage)
        {
            case 1:
                Alert1.ShowMessage(1, MessagesAlert.INSERT_SUCCESSFULL[nDefaulanguage - 1]);
                break;
            case 2:
                Alert1.ShowMessage(1, MessagesAlert.UPDATE_SUCCESSFUL[nDefaulanguage - 1]);
                break;
            default:
                Alert1.HideMessage();
                break;
        }
    }
    #endregion

    #region BindDataToDropDown

    private void textCreateMarginLeft(ref string text, Int16 count)
    {
        if (count > 0)
        {
            string sMargin = String.Empty;
            for (int i = 0; i < count; i++)
            {
                sMargin += "--";
                sMargin += " ";
            }
            text = sMargin + text;
        }
    }

    private void BindDataToLanguageDropDown()
    {
        Languages objLanguage = new Languages();
        DataTable dtbLanguage = new DataTable();
        dtbLanguage = objLanguage.Search();
        ddlLanguage.DataSource = dtbLanguage;
        ddlLanguage.DataValueField = "LanguageID";
        ddlLanguage.DataTextField = "Description";
        ddlLanguage.DataBind();
    }
    #endregion

    #region Grid processing

    protected void dtgBanner_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        int nCurrentLangID = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        int nDefaultLanguage = Support.GetDefaultLanguage();
        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            Label lblTitle = (Label)e.Item.FindControl("lblTitle");
            Label lblType = (Label)e.Item.FindControl("lblType");
            
            LinkButton lnkEdit = (LinkButton)e.Item.FindControl("lnkEdit");
            LinkButton lnkDelete = (LinkButton)e.Item.FindControl("lnkDelete");
            //LinkButton lnkActive = (LinkButton)e.Item.FindControl("lnkActive");
            //LinkButton lnkInactive = (LinkButton)e.Item.FindControl("lnkInactive");
            // TextBox txtPriority = (TextBox)e.Item.FindControl("txtPriority");
            // Label lblID = (Label)e.Item.FindControl("lblID");
            int nID = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "BannerID"));
            int nType=  ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "BannerType"));
            //lblID.Text = ConvertData.ConvertToString(nID);
            //lnkActive.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "BannerID"));
            //lnkActive.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.INACTIVE_ALERT[nDefaultLanguage]);
            lnkDelete.Enabled = true;
            //  lnkActive.Enabled = true;
            switch (nType)
            { 
                case 1:
                    lblType.Text = "Home Page";
                    break;
                case 2:
                    lblType.Text = "Events Page";
                    break;
            }

            switch (nCurrentLangID)
            { 
                case 1:
                    lblTitle.Text= ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Title1"));
                    break;
                case 2:
                    lblTitle.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Title2"));
                    break;
            }

           
            //lblDateModified.Text = ConvertData.ConvertIntToDatetimeString(ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "CreatedDate")));
            //if (nStatus > 0)
            //{
            //    lnkActive.Visible = true;
            //    lnkInactive.Visible = false;
            //}
            //else
            //{
            //    lnkActive.Visible = false;
            //    lnkInactive.Visible = true;
            //}
            string nPriority = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Priority"));

            //  txtPriority.Text = nPriority;




            lnkDelete.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "BannerID"));
            lnkDelete.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.DELETE_ALERT[nCurrentLangID]);

            //lnkInactive.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "BannerID"));
            //lnkInactive.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.ACTIVE_ALERT[nCurrentLangID]);
            lnkEdit.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "BannerID"));

        }
    }



    protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
    {

        BindDataToGrid(1);
    }

    private void BindDataToGrid(int CurrentPageIndex)
    {
        string sSearch = ConvertData.ConvertStringToDB(txtKeyword.Text.Trim());

        int nStatus = ConvertData.ConvertToInt(ddlStatus.SelectedValue);
        int nLanguageID = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        int nDefaultLanguage = Support.GetDefaultLanguage();

        DataTable dtbBanner = new DataTable();
        Banner objBanner = new Banner();
        dtbBanner = objBanner.GetAll();
        lblTotalRecord.Text = MultipleLanguage.LBL_TOTAL_ITEM[nDefaultLanguage - 1] + dtbBanner.Rows.Count + MultipleLanguage.LBL_FOUND[nDefaultLanguage - 1];
        dtgBanner.DataSource = dtbBanner;
        dtgBanner.DataBind();

        Pager1.ItemCount = dtbBanner.Rows.Count;
        VisiblePaging(dtbBanner.Rows.Count);
        if (dtbBanner.Rows.Count > 0)
            tblWarning.Visible = false;
        else
            tblWarning.Visible = true;

        txtKeyword.Focus();
        txtKeyword.Text = ConvertData.RemoveHTMLTag(txtKeyword.Text.Trim());
    }

    protected void dtgBanner_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();
        string strCommand = e.CommandName;
        int nID = ConvertData.ConvertToInt(e.CommandArgument);
        Banner objBanner = new Banner();
        switch (strCommand)
        {
            case "Delete":
                int nDelete = objBanner.DeleteById(nID);
                if (nDelete > 0)
                    Alert1.ShowMessage(1, MessagesAlert.DELETE_SUCCESSFULL[1]);
                else
                    Alert1.ShowMessage(2, MessagesAlert.DELETE_UNSUCCESSFULL[1]);
                BindDataToGrid(Pager1.CurrentIndex);
                break;

            case "Edit":
                string sEdit = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_BANNER_ADD + "&" + Config.ACTION + "=" + Config.ACTION_EDIT + "&" + Config.ACTION_ID + "=" + nID;
                Response.Redirect(sEdit);
                break;

            //case "Active":
            //    int nActive = objBanner.UpdateStatus(nID, EnumeType.INACTIVE);
            //    if (nActive > 0)
            //        Alert1.ShowMessage(1, MessagesAlert.INACTIVE_SUCCESSFUL[1]);

            //    else
            //        Alert1.ShowMessage(2, MessagesAlert.INACTIVE_UNSUCCESSFUL[1]);
            //    BindDataToGrid(Pager1.CurrentIndex);
            //    break;

            //case "Inactive":
            //    int nInactive = objBanner.UpdateStatus(nID, EnumeType.ACTIVE);
            //    if (nInactive > 0)
            //        Alert1.ShowMessage(1, MessagesAlert.ACTIVE_SUCCESSFUL[1]);
            //    else
            //        Alert1.ShowMessage(1, MessagesAlert.ACTIVE_UNSUCCESSFUL[1]);
            //    BindDataToGrid(Pager1.CurrentIndex);
            //    break;
        }
    }

    #endregion

    #region Click Events
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindDataToGrid(1);
    }

    protected void btnAddnew_Click1(object sender, EventArgs e)
    {
        string sAdd = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_BANNER_ADD + "&" + Config.ACTION + "=" + Config.ACTION_ADD;
        Response.Redirect(sAdd);
    }
    #endregion


    protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDataToGrid(1);
    }


    //protected void btnUpdate_Click(object sender, EventArgs e)
    //{


    //    // Update  data
    //    int nCount = dtgBanner.Items.Count;

    //    for (int i = 0; i < nCount; i++)
    //    {
    //        TextBox txtPriority = (TextBox)dtgBanner.Items[i].FindControl("txtPriority");
    //        Label lblID = (Label)dtgBanner.Items[i].FindControl("lblID");

    //        int nBannerID = ConvertData.ConvertToInt(lblID.Text);
    //        int nPriority = ConvertData.ConvertToInt(txtPriority.Text);

    //        Banner objBanner = new Banner();
    //        int nResult = objBanner.UpdatePriority(nBannerID, nPriority);

    //        if (nResult > 0) { Alert1.ShowMessage(1, MessagesAlert.UPDATE_SUCCESSFUL[1]); }
    //        else { Alert1.ShowMessage(2, MessagesAlert.UPDATE_UNSUCCESSFUL[1]); }
    //    }

    //}
}